package cn.legend.travel.ask.controller;

import cn.legend.travel.ask.pojo.param.AddNewAsksParam;
import cn.legend.travel.ask.service.AskService;
import cn.legend.travel.common.pojo.authentication.CurrentPrincipal;
import cn.legend.travel.common.web.JsonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.validation.Valid;
import javax.validation.constraints.Pattern;

@RestController
@RequestMapping("asks")
@Validated
@Slf4j
@Api(tags = "问答模块")
public class AskController {
    @Autowired private AskService service;
    @GetMapping("getAsks")
    @ApiOperation("得到问答")
    public JsonResult getAsks(@Range(min = 0,message = "请提交有效的页码值")Integer pageNum,
                              @RequestParam @Range(min = 0,message = "请提交有效的类型值") Integer type,
                              @AuthenticationPrincipal @ApiIgnore CurrentPrincipal currentPrincipal){
        return JsonResult.ok(service.getAsks(pageNum,type,currentPrincipal));
    }
    @ApiOperation("添加问题")
    @PostMapping("addNewAsk")
    public JsonResult addAsks(@AuthenticationPrincipal @ApiIgnore CurrentPrincipal currentPrincipal,
                              @RequestBody @Valid AddNewAsksParam asksParam){
        service.addAsks(asksParam,currentPrincipal);
        return JsonResult.ok();
    }

    @GetMapping("getBestUsers")
    @ApiOperation("得到最佳回答者列表")
    public JsonResult getBestUsers(@RequestParam @Range(min = 0, message = "请提交有效的时间值！")Integer time,
                                   @RequestParam @Range(min = 0, message = "请提交有效的类型值！")Integer type){
        log.error("time:{},type:{}",time,type);
        return JsonResult.ok(service.getBestUsers(type,time));
    }
    @GetMapping("getDetails")
    @ApiOperation("根据问题Id得到所有回答")
    public JsonResult getAnswers(@RequestParam @Range(min = 1, message = "请提交有效的问题！") Long questionId,
                                 @RequestParam @Range(min = 0, message = "请提交有效的页码！") Integer pageNum){
        log.error("{},{}",questionId,pageNum);
        return JsonResult.ok(service.getAnswers(questionId,pageNum));
    }
    @GetMapping("getAsksById")
    public JsonResult getAskById(@RequestParam @Range(min = 0, message = "请提交有效的问题！") Long questionId){
        log.error("提交的id是{}",questionId);
        return JsonResult.ok(service.getAskById(questionId));
    }
    @ApiOperation("添加点赞数量")
    @PostMapping("like")
    public JsonResult like(@AuthenticationPrincipal @ApiIgnore CurrentPrincipal currentPrincipal,
                           @RequestParam Long questionId){
        service.addLikes(questionId,currentPrincipal);
        return JsonResult.ok();
    }
    @ApiOperation("取消点赞")
    @PostMapping("cancelLike")
    public JsonResult cancelLike(@AuthenticationPrincipal @ApiIgnore CurrentPrincipal currentPrincipal,
                                 @RequestParam Long questionId){
        service.cancelLikes(questionId,currentPrincipal);
        return JsonResult.ok();
    }
    @ApiOperation("添加浏览数量")
    @PostMapping("view")
    public JsonResult view(@RequestParam Long questionId){
        service.AddViews(questionId);
        return JsonResult.ok();
    }
    @ApiOperation("搜索功能")
    @PostMapping("search")
    public JsonResult search(@Pattern(regexp = "^(?! )\\S{1,20}(?<! )$", message = "关键词必须是1~20个字符，且首尾不可以是空格！") String keyword,
                             @Range(min = 1, message = "请提交有效的页码值！") Integer page){
        return JsonResult.ok(service.search(keyword,page));
    }
    @ApiOperation("举报功能")
    @PostMapping("report")
    public JsonResult report(@RequestParam Long asksId){
        service.report(asksId);
        return JsonResult.ok();
    }
    @ApiOperation("根据城市得到问答")
    @GetMapping("getByCity")
    public JsonResult getByCity(String city){
       return JsonResult.ok(service.getByCity(city));

    }
}
